package assignmentonepenguins

import javax.xml.bind.ValidationException
import org.junit.Test
import org.springframework.test.annotation.ExpectedException
import org.codehaus.groovy.grails.exceptions.InvalidPropertyException

class BiddingIntegrationTests {

    @Test
    void test_minimumFiftyCentIncrease() {
        // Create a Seller and a Bidder for a Listing
        Customer customerSeller = new Customer()
        customerSeller.setPassword('abcdefg')
        customerSeller.setEmail('abc123@gmail.com')
        customerSeller.save(flush: true)
        Customer customerBidder = new Customer()
        customerBidder.setPassword('abcdefg')
        customerBidder.setEmail('abc13@gmail.com')
        customerBidder.save(flush: true)

        // Create the Listing
        Listing aListing = new Listing()
        aListing.setDescription("listing 1")
        aListing.setStartBid(1.5)
        aListing.setName("watch")
        aListing.setSeller(customerSeller)
        aListing.setEndDate(new Date() + 7)
        aListing.save(flush: true)
        aListing.refresh()

        // Create three acceptable bids for the Listing
        Bidding bid1 = new Bidding()
        bid1.setAmount(1.0)
        bid1.setBidder(customerBidder)
        bid1.setListing(aListing)
        bid1.save(flush: true)
        Bidding bid2 = new Bidding()
        bid2.setAmount(1.5)
        bid2.setBidder(customerBidder)
        bid2.setListing(aListing)
        bid2.save(flush: true)
        Bidding bid3 = new Bidding()
        bid3.setAmount(2.0)
        bid3.setBidder(customerBidder)
        bid3.setListing(aListing)
        bid3.save(flush: true)
        aListing.refresh()

        // Verify the three bids have been added to the Listing
        aListing.refresh()
        assert aListing.getBiddings().size() == 3
        aListing.refresh()

        // Test that the bid amount must be at least
        // $0.50 higher than the previous bid (B-5)
        Bidding bid4 = new Bidding()
        bid4.setAmount(2.49)
        bid4.setBidder(customerBidder)
        bid4.setListing(aListing)
        bid4.save(flush: true)

        // Verify that the Bidding was not added to the Listing
        // nor saved in the database
        assert !(bid4.validate())
        aListing.refresh()
        assert aListing.getBiddings().size() == 3
    }
}
